/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2016-1-6         yellowlgx
 */

package com.skyworth.ui.listview;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.skyworth.theme.SkyThemeEngine;
import com.skyworth.ui.customview.SlideFocusView;
import com.skyworth.ui.customview.SlideFocusView.FocusChangedEvent;
import com.skyworth.ui.customview.SlideFocusView.FocusViewRevision;

/**
 * @author yellowlgx
 * @ClassName MetroListViewScrollBar
 * @Description MetroListView滚动条，可以改变滚动条样式，需要单独Add到布局中，然后设置到MetroListView中
 * @date 2016-1-6
 */
public class MetroListViewScrollBar extends FrameLayout
{
    /**
     * @Fields focusChangedEvent 公共焦点移动监听
     */
    private FocusChangedEvent focusChangedEvent;
    private FocusViewRevision focusViewRevision;
    private OnFocusChangeListener focusChangeListener;

    private ImageView mScrollBarView;
    private FrameLayout.LayoutParams scrollBarParams;

    private int mScrollBarViewHeight = 0;

    /**
     * @Fields mScrllBarFocusble 滚动条是否可落焦<br/>
     * true表示可获焦，false反之
     */
    private boolean mScrollBarFocusble = false;

    /**
     * @Fields lastPosition 记录上一次移动的位置
     */
    private int lastPosition;

    /**
     * @Fields mSlideFocusView 焦点处理对象，即父容器对象
     */
    private SlideFocusView mSlideFocusView;

    private OnScrollBarScrollListener mListener;

    public interface OnScrollBarScrollListener
    {
        public void onScrollBarScrollPosision(int position);
    }

    public MetroListViewScrollBar(Context context)
    {
        super(context);
        // this.setBackgroundResource(R.drawable.ui_sdk_scroll_bar_bg);

        mScrollBarView = new ImageView(context);
        // mScrollBarView.setImageResource(R.drawable.ui_sdk_scroll_bar);

        scrollBarParams = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT);
        mScrollBarView.setLayoutParams(scrollBarParams);
        this.addView(mScrollBarView);
    }

    public void setOnScrollBarScrollListener(OnScrollBarScrollListener listener)
    {
        mListener = listener;
    }

    public void setSlidFocusView(SlideFocusView focusView)
    {
        mSlideFocusView = focusView;
    }

    /**
     * @return ImageView
     * @Description TODO获取滚动条View<br/>
     * @date 2016-1-6
     */
    public ImageView getScrollBarView()
    {
        return mScrollBarView;
    }

    /**
     * @param focusChangedEvent   监听注册
     * @param rev                 焦点框上下左右位置
     * @param focusChangeListener 外部焦点监听回调
     * @Description 用于使用公共焦点移动框，注册监听<br/>
     * @date 2016-1-4
     */
    public void setFocusChangedEvent(FocusChangedEvent focusChangedEvent, FocusViewRevision rev,
                                     OnFocusChangeListener focusChangeListener)
    {
        this.focusChangedEvent = focusChangedEvent;
        this.focusViewRevision = rev;
        this.focusChangeListener = focusChangeListener;
    }

    /**
     * @param mScrllBarFocusble 设置滚动条是否可获焦
     */
    public void setScrollBarFocusble(boolean mScrollBarFocusble)
    {
        this.mScrollBarFocusble = mScrollBarFocusble;
        if (mScrollBarFocusble)
        {
            mScrollBarView.setFocusable(true);
            mScrollBarView.setFocusableInTouchMode(true);
            focusChangedEvent.registerView(mScrollBarView, focusViewRevision, focusChangeListener);
        } else
        {
            mScrollBarView.setFocusable(false);
        }
    }

    /**
     * @return boolean
     * @Description 获取滚动条是否可获焦状态
     * @date 2016-1-6
     */
    public boolean getScrollBarFocusBle()
    {
        return mScrollBarFocusble;
    }

    /**
     * @return boolean
     * @Description 获取滚动条是否获焦状态
     * @date 2016-1-6
     */
    public boolean hasFocused()
    {
        return mScrollBarView.hasFocus();
    }

    /**
     * @param resid 资源ID
     * @Description 设置滚动条背景<br/>
     * @date 2016-1-6
     */
    public void setScrollBarBg(int resid)
    {
        this.setBackgroundDrawable(SkyThemeEngine.getInstance().getDrawable(resid, false));
    }

    /**
     * @param resid 资源ID
     * @Description 设置滚动条图标<br/>
     * @date 2016-1-6
     */
    public void setScrollBarIcon(int resid)
    {
        BitmapDrawable bd = (BitmapDrawable) getResources().getDrawable(resid);
        if (bd != null)
            mScrollBarViewHeight = bd.getBitmap().getHeight();
        mScrollBarView.setBackgroundResource(resid);
    }
    
    /**
     * @param resid
     *            资源ID
     * @Description 设置滚动条图标<br/>
     * @date 2017-08-09
     */
    public void setScrollBarIcon(int resid, boolean addColor)
    {
        BitmapDrawable bd = (BitmapDrawable) getResources().getDrawable(resid);
        if (bd != null)
            mScrollBarViewHeight = bd.getBitmap().getHeight();
        mScrollBarView
                .setBackgroundDrawable(SkyThemeEngine.getInstance().getDrawable(resid, addColor));
    }

    /**
     * @param position 具体位置
     * @Description 设置滚动条具体滚动到的位置<br/>
     * 此位置是距离顶部的距离<br/>
     * @date 2016-1-6
     */
    public void setScrollBarPosition(int position)
    {
        scrollBarParams.topMargin += (position - lastPosition);
        int temp = 0;
        if (scrollBarParams.topMargin < 0)
        {
            temp = scrollBarParams.topMargin;
            scrollBarParams.topMargin = 0;
        }
        mScrollBarView.setLayoutParams(scrollBarParams);
        if (mListener != null)
            mListener.onScrollBarScrollPosision(scrollBarParams.topMargin);

        if (mSlideFocusView != null && mScrollBarView.hasFocus())
        {
            // LayoutParams frameParams = (LayoutParams) mSlideFocusView.getFocusView()
            // .getLayoutParams();
            // if (scrollBarParams.topMargin == 0)
            // {
            // frameParams.topMargin += (position - lastPosition - temp);
            // } else
            // {
            // frameParams.topMargin += (position - lastPosition);
            // }
            // mSlideFocusView.getFocusView().setLayoutParams(frameParams);
            mSlideFocusView.stopAnimationOnce();
            mSlideFocusView.moveFocusByCurrent(0, position - lastPosition - temp);
        }
        lastPosition = position;
    }

    public void setScrollBarToPosition(int position)
    {
        lastPosition = 0;
        scrollBarParams.topMargin = position;
        mScrollBarView.setLayoutParams(scrollBarParams);
        if (mListener != null)
            mListener.onScrollBarScrollPosision(scrollBarParams.topMargin);
    }

    public void setScrollBar_focusViewPosition(int position)
    {

        lastPosition = 0;
        scrollBarParams.topMargin = position;
        mScrollBarView.setLayoutParams(scrollBarParams);
        if (mListener != null)
            mListener.onScrollBarScrollPosision(scrollBarParams.topMargin);

        mScrollBarView.post(new Runnable()
        {
            @Override
            public void run()
            {
                Log.v("lgx", "setScrollBar_focusViewPosition=====>");
                if (mSlideFocusView != null)
                    mSlideFocusView.moveFocusTo(mScrollBarView, focusViewRevision);
            }
        });
    }

    public void setDefaultLastPosition()
    {
        lastPosition = 0;
    }

    /**
     * @return int
     * @Description 获取滚动条总高度<br/>
     * @date 2016-1-6
     */
    public int getScrollBarHeight()
    {
        ViewGroup.LayoutParams params = this.getLayoutParams();
        if (params != null)
            return params.height;
        return 0;
    }

    /**
     * @return int
     * @Description 获取滚动条ScrollBar高度<br/>
     * @date 2016-1-6
     */
    public int getScrollBarViewHeight()
    {
        return mScrollBarViewHeight;
    }

    /**
     * @return int
     * @Description 获取当前滚动条位置<br/>
     * @date 2016-1-6
     */
    public int getScrollBarCurrentY()
    {
        return scrollBarParams.topMargin;
    }

}
